package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Permission;
import cn.itcast.service.PermissionService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
@CrossOrigin
public class PermissionController {
    @Reference
    private PermissionService permissionService;

    /**
     * 根据角色查询权限
     * @param rid
     * @return
     */
    @GetMapping("role/{rid}")
    public Result findByRid(@PathVariable("rid") Integer rid){
        try {
            List<Permission> list = permissionService.findByRid(rid);

            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    /**
     * 查询菜单详情
     * @param menusIds
     * @return
     */
    @GetMapping(path = "/menu/association")
    public Result findMenuAssociation(Integer menusIds) {
        try {
            List<Permission> permissions = permissionService.findMenuAssociation(menusIds);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permissions);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }


    /**
     * 添加权限
     *
     * @param permission
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Permission permission) {
        try {
            permissionService.add(permission);
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
        }
    }

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping
    public Result findPage(QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = permissionService.selectByCondition(queryPageBean);

            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    /**
     * 查询用户id
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result findById(@PathVariable Integer id) {
        try {
            Permission findById = permissionService.findById(id);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, findById);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    /**
     * 删除用户id
     */
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable Integer id) {
        try {
            permissionService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
        }
    }

    /**
     * 修改权限
     *
     * @param permission
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Permission permission) {
        try {
            permissionService.update(permission);
            return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL);
        }

    }

    @GetMapping("all")
    public Result findAll() {
        try {
            List<Permission> permissionList = permissionService.findAll();
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permissionList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }


    }
}


